<?php

namespace app\api\model;

use think\Model;

class LandmarkProtect extends Model
{

    protected $add_rule =   [
        'product_id'   => 'require',
        'name'   => 'require',
//        'believe_organ'       =>'require',
//        'believe_measure'       =>'require',
//        'believe_time' =>'require',
    ];

    protected $add_message  =   [
        'product_id.require' => '所属产品不能为空',
        'name.require' => '请选择认定保护',
//        'believe_organ.require' => '认定时机构不能为空',
//        'believe_measure.require' => '认定标准不能为空',
//        'believe_time.require' => '认定时间不能为空',
    ];

    public function add_vali($data)
    {

        if($data['status'] == 1){
            $info = $this->where('status',1)->where('product_id',$data['product_id'])->find();
            if($info){
                return re_error('只能有一个地里标志保护产品');
            }
        }else{
            $info = $this->where('status',1)->where('product_id',$data['product_id'])->find();
            if($info['status'] == $data['status']){
                return re_error('只能有一个地里标志保护产品');
            }
        }

        try {
            validate($this->add_rule,$this->add_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function add($data)
    {
        $data['time'] = date('Y-m-d H:i:s');
        $this->startTrans();
        try {
            $this->allowField([
                'time',
                'user_id',
                'status',
                'name',
                'code',
                'ratify_time',
                'application_organ',
                'region',
                'protection_range',
                'protection_image',
                'protection_json',
                'protection_code',
                'image',
                'product_id',
                'company_id',

            ])->save($data);
            $this->commit();
            return re_success("添加成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $edit_rule =   [
        'id'   => 'require',
        'name'   => 'require',
//        'believe_organ'       =>'require',
//        'believe_measure'       =>'require',
//        'believe_time' =>'require',
    ];

    protected $edit_message  =   [
        'id.require'=>'缺失的标识',
        'name.require' => '请输入认定保护',
//        'believe_organ.require' => '认定时机构不能为空',
//        'believe_measure.require' => '认定标准不能为空',
//        'believe_time.require' => '认定时间不能为空',
    ];
    public function edit_vali($data)
    {

        try {
            validate($this->edit_rule,$this->edit_message)->check($data);
            if($data['status'] == 1){
                $info = $this->where('product_id',$data['product_id'])->where('status',1)->where('id','<>',$data['id'])->where('name',$data['name'])->find();
                if($info){
                    return re_error('只能有一个地里标志保护产品');
                }
            }


        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    protected $edit_status_rule =   [
        'id'   => 'require',
        'product_id'   => 'require',
        'status'   => 'require'
    ];

    protected $edit_status_message  =   [
        'id.require'=>'缺失',
        'product_id.require'=>'缺失的产品',
        'status.require' => '状态不能为空',
    ];
    public function edit_status_vali($data)
    {

        try {
            validate($this->edit_status_rule,$this->edit_status_message)->check($data);
            $info = $this->where('product_id',$data['product_id'])->where('id','<>',$data['id'])->find();
            if($info){
                return re_error('只能有一个地里标志保护产品');
            }

        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function editStatus($data)
    {

        $model = LandmarkProtect::find($data['id']);
        $model->startTrans();
        try {
            $model->allowField([
                'status'
            ])->save($data);
            $model->commit();
            return re_success("操作成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    public function edit($data)
    {
        $model = LandmarkProtect::find($data['id']);
        $model->startTrans();
        try {

            $model->allowField([
                'time',
                'user_id',
                'status',
                'name',
                'code',
                'ratify_time',
                'application_organ',
                'region',
                'protection_range',
                'protection_image',
                'protection_json',
                'protection_code',
                'image',
            ])->save($data);
            $model->commit();
            return re_success("修改成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $dele_rule =   [
        'id'   => 'require',
        'product_id'=>'require',
    ];

    protected $dele_message  =   [
        'id.require'=>'缺失的参数',
        'product_id.require'=>'缺失的产品',
    ];
    public function dele_vali($data)
    {

        try {

            validate($this->dele_rule,$this->dele_message)->check($data);
            $info = $this->find($data['id']);
            if($info['product_id'] !== $data['product_id']){
                return re_warning('已记录非法删除他人产品');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function dele($id)
    {

        $model = LandmarkProtect::find($id);

        $model->startTrans();
        try {
            $model->delete();
            $model->commit();
            return re_success("删除成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
}